/*********************************************************************************
 *      Copyright:  (C) 2023 Xie Mujie<1289180278@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  conf_json.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2023年07月18日)
 *         Author:  Xie Mujie <1289180278@qq.com>
 *      ChangeLog:  1, Release initial version on "2023年07月18日 20时14分21秒"
 *                 
 ********************************************************************************/
#include "main.h"

int set_conf_json(char *confname, char *hostname, int port, char *clientid, char *username, char *passwd)
{
	int             fd;
	int             size;
	char           *json_data;
	cJSON          *item;

	item = cJSON_CreateObject();

	cJSON_AddStringToObject(item, "log_path", LOG_PATH);
	cJSON_AddStringToObject(item, "hostname", hostname);
	cJSON_AddNumberToObject(item, "port", port);
	cJSON_AddStringToObject(item, "clientid", clientid);
	cJSON_AddStringToObject(item, "username", username);
	cJSON_AddStringToObject(item, "passwd", passwd);
	cJSON_AddStringToObject(item, "subtopic", SUBTOPIC);
	cJSON_AddStringToObject(item, "pubtopic", PUBTOPIC);
	cJSON_AddNumberToObject(item, "qos", QOS);
	cJSON_AddNumberToObject(item, "keepalive", KEEPALIVE);
	cJSON_AddStringToObject(item, "method", METHOD);
	cJSON_AddStringToObject(item, "id", ID);
	cJSON_AddStringToObject(item, "identifier", IDENTIFIER);
	cJSON_AddStringToObject(item, "version", VERSION);

	json_data = cJSON_Print(item);

	fd = open(confname, O_RDWR|O_CREAT, 0664);
	if (fd < 0)
	{
		printf("conf.json open failure: %s\n", strerror(errno));
		return -1;
	}

	size = write(fd, json_data, strlen(json_data));
	if (size < 0)
	{
		printf("write conf.json failure: %s\n", strerror(errno));
		return -2;
	}

	close(fd);
	cJSON_Delete(item);
	free(json_data);

	return 0;
}

int pari_conf_json(char *confname, mqtt_conf_t *conf_data)
{
	FILE             *f;
	long              len;
	char             *json_data;
	cJSON            *cjson;

	f = fopen(confname, "rb");
	if (NULL == f)
	{
		printf("open conf.json failure: %s\n", strerror(errno));
		return -1;
	}

	fseek(f, 0, SEEK_END);
	len = ftell(f);
	fseek(f, 0, SEEK_SET);
	
	json_data = (char *)malloc(len+1);
	fread(json_data, 1, len, f);

	fclose(f);
	
	cjson = cJSON_Parse(json_data);

	strncpy(conf_data->log_path, cJSON_GetObjectItem(cjson, "log_path")->valuestring, sizeof(conf_data->log_path));

	strncpy(conf_data->hostname, cJSON_GetObjectItem(cjson, "hostname")->valuestring, sizeof(conf_data->hostname));

	conf_data->port = cJSON_GetObjectItem(cjson, "port")->valueint;

	strncpy(conf_data->clientid, cJSON_GetObjectItem(cjson, "clientid")->valuestring, sizeof(conf_data->clientid));

	strncpy(conf_data->username, cJSON_GetObjectItem(cjson, "username")->valuestring, sizeof(conf_data->username));

	strncpy(conf_data->passwd, cJSON_GetObjectItem(cjson, "passwd")->valuestring, sizeof(conf_data->passwd));

	strncpy(conf_data->subtopic, cJSON_GetObjectItem(cjson, "subtopic")->valuestring, sizeof(conf_data->subtopic));

	strncpy(conf_data->pubtopic, cJSON_GetObjectItem(cjson, "pubtopic")->valuestring, sizeof(conf_data->pubtopic));

	conf_data->qos = cJSON_GetObjectItem(cjson, "qos")->valueint;

	conf_data->keepalive = cJSON_GetObjectItem(cjson, "keepalive")->valueint;

	strncpy(conf_data->method, cJSON_GetObjectItem(cjson, "method")->valuestring, sizeof(conf_data->method));

	strncpy(conf_data->identifier, cJSON_GetObjectItem(cjson, "identifier")->valuestring, sizeof(conf_data->identifier));

	strncpy(conf_data->version, cJSON_GetObjectItem(cjson, "version")->valuestring, sizeof(conf_data->version));

	return 0;
}
